package com.bst.bsj.bops.web.biz.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bst.bsj.bops.web.biz.MerchantService;
import com.bst.bsj.common.dal.daointerface.MerchantDAO;
import com.bst.bsj.common.dal.dataobject.Merchant;
import com.homewaimai.common.lang.Paginator;
import com.homewaimai.common.lang.Result;
import com.homewaimai.common.lang.ResultSupport;

@Service("merchantService")
public class MerchantServiceImpl implements MerchantService {

  private static final Log logger = LogFactory.getLog(MerchantServiceImpl.class);

  @Autowired
  private MerchantDAO merchantDao;
  
  @Override
  public Result query(Map paramMap) {
    Result result = new ResultSupport(false);
    
    Long offset = (Long) paramMap.get("offset");
    Long length = Long.parseLong(paramMap.get("length")+"");
    Integer page = (Integer) paramMap.get("page");
    Long total = merchantDao.getCounts(paramMap);
    List<Merchant> merchantList = merchantDao.selectAll(paramMap);

 
    // 创建一个分页器(leng.intValue：每页多少条)
    Paginator paginator = new Paginator(length.intValue());
    // 设置总项数
    paginator.setItems(total == null ? 0 : total.intValue());
    // 设置当前页
    paginator.setPage(page);
    result.getModels().put("paginator", paginator);
    result.getModels().put("merchantList", merchantList);

    result.setSuccess(true);
    return result;
  }

  @Override
  public int delete(Long id) {
    int lid = 0;
    try {

      lid = merchantDao.delete(id);

    } catch (Exception e) {
      logger.error("merchantServiceImpl.delete(id) error!", e);
    }

    return lid;
  }

  @Override
  public Merchant query(Long id) {
    return merchantDao.query(id);
  }

  @Override
  public int update(Merchant member) {
    int lid = 0;
    try {

      lid = merchantDao.update(member);
    } catch (Exception e) {
      logger.error("merchantServiceImpl.update(banner) error!", e);
    }
    return lid;
  }

  @Override
  public Long save(Merchant member) {
    Long lid = 0l;
    try {
      
      lid = merchantDao.save(member);
    } catch (Exception e) {
      logger.error("merchantServiceImpl.save(banner) error!", e);
    }

    return lid;
  }

  @Override
  public Merchant selectByMobile(String mobile) {
    return merchantDao.selectByMobile(mobile);
  }

  @Override
  public List<Merchant> getMerchantByRoleId(Long roleId) {
    
    return merchantDao.getMerchantByRoleId(roleId);
  }


  
}
